package com.jd.basic.controller;

import com.jd.basic.dto.RegisterDto;
import com.jd.basic.dto.RegisterEmailDto;
import com.jd.basic.dto.VerifyEmailDto;
import com.jd.basic.service.VerifyCodeService;
import com.jd.basic.utils.AjaxResult;
import com.jd.basic.dto.VerifyDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult getCode(@PathVariable("imageCodeKey") String imageCodeKey) {
        try {
            String str = verifyCodeService.getCode(imageCodeKey);
            return AjaxResult.me().success().setResultObj(str);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail().setMsg(e.getMessage());
        }
    }
    // 发送短信验证码接口
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody @Validated VerifyDto verifyDto) {
        try {
            verifyCodeService.smsCode(verifyDto);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }

    // 手机注册接口
    @PostMapping("/phoneRegister")
    public AjaxResult phoneRegister(@RequestBody @Validated RegisterDto registerDto) {
        try {
            verifyCodeService.phoneRegister(registerDto);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }
    // 邮箱注册接口
    @PostMapping("/emailRegister")
    public AjaxResult emailRegister(@RequestBody @Validated RegisterEmailDto registerEmailDto) {
        try {
            verifyCodeService.emailRegister(registerEmailDto);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }
    // 发送邮箱验证码接口
    @PostMapping("/emailSmsCode")
    public AjaxResult sendEmailSmsCode(@RequestBody @Validated VerifyEmailDto dto) {
        try {
            verifyCodeService.sendEmailSmsCode(dto);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }

}
